package com.example.contact_recover;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.contact_recover.models.ServerRes;
import com.example.contact_recover.models.User;
import com.example.contact_recover.util.NetUtil;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class SignActivity extends AppCompatActivity {

    private EditText editPhone;
    private EditText editPasswd;
    private Button btnSign;
    private String phone;
    private String passwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setTitle(getResources().getString(R.string.title_sign));

        editPhone = findViewById(R.id.edit_phone);
        editPasswd = findViewById(R.id.edit_passwd);


        btnSign = findViewById(R.id.btn_sign);

        btnSign.setOnClickListener((v) -> {
            phone = editPhone.getText().toString().trim();
            passwd = editPasswd.getText().toString().trim();
            System.out.println("phone "+ phone+", passwd = "+passwd);
            //
            NetUtil.Commit2Server(
                    NetUtil.SERVER_PULL + "/user/sign",
                    phone,
                    passwd,
                    new signCall()
            );
        });


    }


    // another thread
    class signCall implements Callback {

        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {

            byte[] bytes = response.body().bytes();

            String text = new String(bytes, 0, bytes.length);

            ServerRes serverRes = JSON.parseObject(text, ServerRes.class);

            System.out.println("serverRes " + serverRes);

            if (serverRes == null) {
                return;
            }

            if (serverRes.getCode() != ServerRes.Ok) {
               runOnUiThread(()->{
                   Toast.makeText(SignActivity.this, serverRes.getMessage(), Toast.LENGTH_SHORT).show();
               });
            } else {

                runOnUiThread(()->{
                    SharedPreferences lastLogin = getSharedPreferences("last_login", MODE_PRIVATE);

                    SharedPreferences.Editor editor = lastLogin.edit();

                    // todo 这里需要做加密处理
                    editor.putString(getResources().getString(R.string.title_phone), phone);
                    editor.putString(getResources().getString(R.string.title_passwd), passwd);
                    User user = new User();
                    user.setPasswd(passwd);
                    user.setPhone(phone);
                    user.save();
                    editor.putLong("id",user.getId());
                    // 保存 登录信息
                    editor.apply();

                    finish();

                    startActivity(new Intent(SignActivity.this, MainActivity.class));
                });

            }
        }
    }
}
